package com.orangelife.service.propFeeAmountLog.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeAmountLog.IPropFeeAmountLogService;
import com.orangelife.service.propFeeAmountLog.model.PropFeeAmountLogDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeAmountLog类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-08-22 16:40:45
 * ==============================================================
 */
@Service("propFeeAmountLogService")
public class PropFeeAmountLogServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeAmountLogService {
	private static Logger logger = Logger
			.getLogger(PropFeeAmountLogServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeAmountLogServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeAmountLog(PropFeeAmountLogDto propFeeAmountLog)
			throws AppBizException {

		Assert.notNull(propFeeAmountLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeAmountLog.getOperation())) {
			if (propFeeAmountLog.getOperation().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"操作(1加2,减)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAmountLog.getRemark())) {
			if (propFeeAmountLog.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAmountLog.getLogContent())) {
			if (propFeeAmountLog.getLogContent().length() > 512) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"json格式 操作的一些具体信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAmountLog.getCreateuser())) {
			if (propFeeAmountLog.getCreateuser().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "创建者长度超出");
			}
		}

		propFeeAmountLog.setLogID(IdUtil.getuuId());
		propFeeAmountLog.setItemStat(TypeConst.FLAG_ON);
		propFeeAmountLog.setTscreate(new Date());
		propFeeAmountLog.setTimes(String.valueOf(propFeeAmountLog.getTscreate()
				.getTime()));
		return this.getSqlSession().insert(
				"PropFeeAmountLogMapper.insertSelective", propFeeAmountLog);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeAmountLog(PropFeeAmountLogDto propFeeAmountLog)
			throws AppBizException {

		Assert.notNull(propFeeAmountLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeAmountLog.getLogID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeAmountLogDto oldPropFeeAmountLog = this
				.findPropFeeAmountLogByID(propFeeAmountLog.getLogID());

		if (!StringUtils.isBlank(propFeeAmountLog.getOperation())) {
			if (propFeeAmountLog.getOperation().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"操作(1加2,减)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAmountLog.getRemark())) {
			if (propFeeAmountLog.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAmountLog.getLogContent())) {
			if (propFeeAmountLog.getLogContent().length() > 512) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"json格式 操作的一些具体信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAmountLog.getCreateuser())) {
			if (propFeeAmountLog.getCreateuser().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "创建者长度超出");
			}
		}

		if (oldPropFeeAmountLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		return this.getSqlSession().update(
				"PropFeeAmountLogMapper.updateByPrimaryKeySelective",
				propFeeAmountLog);
	}

	@Override
	public PropFeeAmountLogDto findPropFeeAmountLogByID(
			String propFeeAmountLogID) throws AppBizException {
		if (StringUtils.isBlank(propFeeAmountLogID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAmountLogDto oldPropFeeAmountLog = this.getSqlSession()
				.selectOne("PropFeeAmountLogSearchMapper.selectByPrimaryKey",
						propFeeAmountLogID);
		if (oldPropFeeAmountLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeAmountLog.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeAmountLog;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeAmountLogByLogic(String propFeeAmountLogID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeAmountLogID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAmountLogDto oldPropFeeAmountLog = this
				.findPropFeeAmountLogByID(propFeeAmountLogID);

		oldPropFeeAmountLog.setItemStat(TypeConst.FLAG_OFF);

		return this.getSqlSession().update(
				"PropFeeAmountLogMapper.updateByPrimaryKeySelective",
				oldPropFeeAmountLog);
	}

	@Override
	public List<Map<String, Object>> findPropFeeAmountLogByPage(
			String accountID, String operation, String logType,
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("accountID", accountID);
		map.put("operation", operation);

		if (!StringUtils.isBlank(logType)) {
			String feeTypeIDs[] = logType.split("#");
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < feeTypeIDs.length - 1; i++) {
				buffer.append("'").append(feeTypeIDs[i]).append("'")
						.append(",");
			}
			buffer.append("'").append(feeTypeIDs[feeTypeIDs.length - 1])
					.append("'");
			map.put("logTypes", buffer.toString());

		}
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAmountLogSearchMapper.findAllPropFeeAmountLog", map);
		return list;
	}

	@Override
	public List<PropFeeAmountLogDto> getPropFeeAmountLogByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeAmountLogDto> list = this.getSqlSession().selectList(
				"PropFeeAmountLogSearchMapper.getAllPropFeeAmountLogDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeAmountLogByRelevantID(
			String logID, String accountID, String logType, String linkID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("logID", logID);
		map.put("accountID", accountID);
		map.put("logType", logType);
		map.put("linkID", linkID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAmountLogSearchMapper.selectPropFeeAmountLogDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeAmountLogDto> findPropFeeAmountLogByRelevantID(
			String logID, String accountID, String logType, String linkID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("logID", logID);
		map.put("accountID", accountID);
		map.put("logType", logType);
		map.put("linkID", linkID);

		List<PropFeeAmountLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAmountLogSearchMapper.findPropFeeAmountLogDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeAmountLogDto> bauthfindPropFeeAmountLog(
			List<String> propFeeAmountLogIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeAmountLogDto> list = this.getSqlSession().selectList(
				"PropFeeAmountLogSearchMapper.batchFindPropFeeAmountLog",
				propFeeAmountLogIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeAmountLogByMap(
			Map<String, Object> propFeeAmountLog) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAmountLogSearchMapper.batchFindPropFeeAmountLogByMap",
				propFeeAmountLog);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeAmountLog(List<PropFeeAmountLogDto> propFeeAmountLog)
			throws AppBizException {
		if (propFeeAmountLog != null && propFeeAmountLog.size() > 0) {
			return this.getSqlSession()
					.insert("PropFeeAmountLogBauthMapper.bauthInsert",
							propFeeAmountLog);
		}
		return 0;
	}

	@Override
	public List<Map<String, Object>> findPropFeeAmountLogs(String accountID,
			String operation, String logType, String search, String tsCreate,
			int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("accountID", accountID);
		map.put("operation", operation);
		if (!StringUtils.isBlank(logType)) {
			String feeTypeIDs[] = logType.split("#");
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < feeTypeIDs.length - 1; i++) {
				buffer.append("'").append(feeTypeIDs[i]).append("'")
						.append(",");
			}
			buffer.append("'").append(feeTypeIDs[feeTypeIDs.length - 1])
					.append("'");
			map.put("logTypes", buffer.toString());

		}
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAmountLogSearchMapper.findPropFeeAmountLog", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> findPropFeeAmountLogsForExport(String accountID,
			String operation, String logType, String search, String tsCreate,
			int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("accountID", accountID);
		map.put("operation", operation);
		if (!StringUtils.isBlank(logType)) {
			String feeTypeIDs[] = logType.split("#");
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < feeTypeIDs.length - 1; i++) {
				buffer.append("'").append(feeTypeIDs[i]).append("'")
						.append(",");
			}
			buffer.append("'").append(feeTypeIDs[feeTypeIDs.length - 1])
					.append("'");
			map.put("logTypes", buffer.toString());

		}
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAmountLogSearchMapper.findPropFeeAmountLogForExport", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> findPropFeeAmountLogs(String accountID,
			String operation, String logType, String search)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("search", search);
		map.put("accountID", accountID);
		map.put("operation", operation);
		if (!StringUtils.isBlank(logType)) {
			String feeTypeIDs[] = logType.split("#");
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < feeTypeIDs.length - 1; i++) {
				buffer.append("'").append(feeTypeIDs[i]).append("'")
						.append(",");
			}
			buffer.append("'").append(feeTypeIDs[feeTypeIDs.length - 1])
					.append("'");
			map.put("logTypes", buffer.toString());

		}

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAmountLogSearchMapper.findPropFeeAmountLog", map);
		return list;
	}

}